﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Google.GData.Calendar;
using System.Diagnostics;
using Google.GData.Contacts;

namespace GSync
{
    public class GServerUtil
    {
        public static bool TestGmailAccount(string accountName, string passWord)
        {
            bool result = false;
            try
            {
                ContactsService service = new ContactsService(GSyncFactory.Instance.SettingsManager.AppVersion);
                service.setUserCredentials(accountName, passWord);
                ContactsQuery query = new ContactsQuery(ContactsQuery.CreateContactsUri("default"));
                query.UseSSL = true;
                ContactsFeed feed = service.Query(query);


                int entrycount = feed.Entries.Count;
                result = true;
                
            }
            catch (Exception ex)
            {
                GSyncFactory.Instance.Logger.Write("An error occured while trying to access gmail account: " + ex.ToString(), "error");

            }

            return result;
        }

        public static bool TestCalendarAccount(string accountName, string passWord)
        {
            bool result = false;
            try
            {
                // Create a CalenderService and authenticate
                CalendarService service = new CalendarService(GSyncFactory.Instance.SettingsManager.AppVersion);
                service.setUserCredentials(accountName, passWord);

                CalendarQuery query = new CalendarQuery("http://www.google.com/calendar/feeds/default/owncalendars/full");
                query.UseSSL = true;
                CalendarFeed resultFeed = service.Query(query);
                result = resultFeed.Entries.Count > 0;
            }
            catch (Exception ex)
            {
                GSyncFactory.Instance.Logger.Write("An error occured while trying to get user calendars: " + ex.ToString(), "error");
            }

            return result;


           

        }
    }
}
